package com.tgpms.interfaces.common;

import com.alibaba.fastjson.JSON;
import com.tgpms.common.Result;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.exception.QueryException;
import io.netty.util.internal.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 * @author May
 * <p>
 * 处理公共的请求处理方法，必定在Controller数据交互中减少逻辑的处理
 */
public class CommonRequestDispose {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 读/写操作 公共处理方法
     *
     * @param data
     * @return
     */
    public Result commonDispose(String data) {
        Result result = new Result();
        if (StringUtil.isNullOrEmpty(data) || "not_login".equals(data)) {
            result.setSuccess(false);
            result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
            result.setData(data);
            return result;
        }
        Object parseObject = null;
        try {
            parseObject = JSON.parseObject(data, Object.class);
        } catch (Exception e) {
            throw new QueryException(ExceptionExplain.ERROR_BY_PARSING.getExplain());
        }
        result.setData(parseObject);
        return result;
    }

}
